package sfeir.ergosum.adapter;

import java.util.ArrayList;

import sfeir.ergosum.R;
import sfeir.ergosum.model.NetworkItem;
import sfeir.ergosum.model.Person;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NetworkListAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<NetworkItem> networks;
	private Person p;
	private ViewHolder holder;

	public NetworkListAdapter(Context c, Person p) {
		mContext = c;
		this.p=p;
		mInflater = LayoutInflater.from(mContext);
		networks = new ArrayList<NetworkItem>();
		populate();
	}
	
	public void update(Person newP){
		p = newP;
		networks.clear();
		populate();
		notifyDataSetChanged();
	}

	private void populate() {
		for (NetworkItem ni : p.getNetworkItems())
			networks.add(ni);
	}

	private static class ViewHolder {
		ImageView logo;
		TextView link;
	}

	@Override
	public int getCount() {
		return networks.size();
	}

	@Override
	public Object getItem(int position) {
		return networks.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, null);

			holder = new ViewHolder();
			holder.logo = (ImageView) convertView.findViewById(R.id.list_item_image);
			holder.link = (TextView) convertView.findViewById(R.id.list_item_content);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		NetworkItem ni = (NetworkItem) getItem(position);

		String logo = ni.getNetworkItemType();

		if (logo.equalsIgnoreCase("linkedin"))
			holder.logo.setImageResource(R.drawable.linkedin);
		else if (logo.equalsIgnoreCase("twitter"))
			holder.logo.setImageResource(R.drawable.twitter);
		else if (logo.equalsIgnoreCase("facebook"))
			holder.logo.setImageResource(R.drawable.facebook);
		else if (logo.equalsIgnoreCase("viadeo"))
			holder.logo.setImageResource(R.drawable.viadeo);
		else if (logo.equalsIgnoreCase("skype"))
			holder.logo.setImageResource(R.drawable.skype);
		else if (logo.equalsIgnoreCase("flickr"))
			holder.logo.setImageResource(R.drawable.flickr);
		else if (logo.equalsIgnoreCase("vimeo"))
			holder.logo.setImageResource(R.drawable.vimeo);
		else if (logo.equalsIgnoreCase("dopplr"))
			holder.logo.setImageResource(R.drawable.dopplr);
		else if (logo.equalsIgnoreCase("lastfm"))
			holder.logo.setImageResource(R.drawable.lastfm);
		else if (logo.equalsIgnoreCase("readernaut"))
			holder.logo.setImageResource(R.drawable.readernaut);
		else if (logo.equalsIgnoreCase("friendfeed"))
			holder.logo.setImageResource(R.drawable.friendfeed);
		

		holder.link.setText(ni.getNetworkItemLink());
		holder.link.setClickable(true);
		holder.link.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				WebView webview = new WebView(mContext);
				webview.getSettings().setJavaScriptEnabled(true);
				webview.loadUrl(((TextView)v).getText().toString());
				
				Builder dialog = new AlertDialog.Builder(mContext);
				dialog.setView(webview);
				dialog.show();
				
			}
		});

		return convertView;
	}
}
